<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *职位管理
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$tabs=array(
	array('op'=>'list','title'=>'管家列表'),
	array('op'=>'edit','title'=>'添加管家'),	
);
if($_RQ['op']=='list'){
	$params=array();
	$params['uniacid']=$_SESSION['uniacid'];
	if(!empty($_RQ['title'])){
		$params['name like']='%'.trim($_RQ['name']).'%';
		$return['params']['name']=$_RQ['name'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$fields=array('id','uniacid','name','avatar','mobile','bid','mid','score','status','createtime');
	$list=pdo_getslice('jy_smallquan_manager',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){

		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','edit'=>true,'list'=>array(
			array('name'=>'name','title'=>'管家姓名','type'=>'string'),
			array('name'=>'avatar','title'=>'管家头像','type'=>'image'),
			array('name'=>'score','title'=>'评分','type'=>'string'),
			array('name'=>'status','title'=>'状态','type'=>'switch','set'=>'set'),
		)),
		'search'=>array(
			array('name'=>'name','width'=>15,'title'=>'管家姓名','type'=>'string'),
		)
	);
	$return['htmls']=$htmls;
	exi($return);
}

if($_RQ['op']=='edit'){
	$return=array();
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_smallquan_manager',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		
	}else{
		$item=array();
	}
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回列表'),
		),
		'edit_tab'=>true,
		'edits'=>array(
			array(
				'title'=>'基本信息',
				'list'=>array(
					array('name'=>'name','title'=>'姓名','type'=>'string'),
					array('name'=>'avatar','title'=>'头像','type'=>'image'),
					array('name'=>'mobile','title'=>'联系电话','type'=>'string'),
					array('name'=>'bid','title'=>'选择管理楼宇','type'=>'select','options'=>'bid'),
					array('type'=>'choose','title'=>'选择发布人','name'=>'mid','action'=>'get_member','choose_title'=>'选择会员'),
					array('name'=>'status','title'=>'启用','type'=>'switch'),
				)
			),
		)
	);
	$buildings=pdo_getall('jy_smallquan_building',array('uniacid'=>$_SESSION['uniacid']));
	if($buildings){
		foreach($buildings as &$building){
			$building['title']=$building['area'].$building['qishu'].$building['dongshu'].'栋';
		}
	}
	//print_r($return['lev_name']);exit;
	$return['bid']=$buildings;
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	if(empty($postdata['name'])){
		exi('请完善姓名','error');
	}
	if(empty($postdata['avatar'])){
		exi('请完善头像','error');
	}
	if(empty($postdata['mobile'])){
		exi('请完善联系电话','error');
	}
	if(!$postdata['bid']){
		exi('请选择管理楼宇','error');
	}
	if(!$postdata['mid']){
		exi('请选择发布人','error');
	}
	$data=array(
		'name'=>trim($postdata['name']),	
		'avatar'=>trim($postdata['avatar']),	
		'mobile'=>trim($postdata['mobile']),	
		'bid'=>intval($postdata['bid']),	
		'mid'=>intval($postdata['mid']),	
		'status'=>intval($postdata['status']),	
	);
	$id=intval($postdata['id']);
	if($id){
		pdo_update('jy_smallquan_manager',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		$data['createtime']=TIMESTAMP;
		pdo_insert('jy_smallquan_manager',$data);
	}
	exi('操作成功！','','list');
}
if($_RQ['op']=='del'){
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
	}else{
		$del=array();
	}
	if($_RQ['id']){
		$del[]=$_RQ['id'];
	}
	foreach($del as $id){
		$id=intval($id);
		if($id){
			pdo_delete('jy_smallquan_manager',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('删除成功！');
}
if($_RQ['op']=='set'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择要编辑的管家！','error');
	}
	$post=get_postdata();
	if(isset($post['status'])){
		pdo_update('jy_smallquan_manager',array('status'=>$post['status']),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	exi('操作成功！');
}